﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace VideoTranscode
{
    class Program
    {
        enum VideoArgs
        {            
            OutputDirectory = 0,

        }
        static void Main(string[] args)
        {            
            string outputDirectory = args[(int)VideoArgs.OutputDirectory].ToString();
            DirectoryInfo dirInfo = new DirectoryInfo(outputDirectory);
            FileInfo[] videoFiles = dirInfo.GetFiles("*.mkv");
            
            foreach (FileInfo video in videoFiles)
	        {
		        TranscodeVideoFile(video);
	        }
        }

        private static void TranscodeVideoFile(FileInfo video)
        {
            try
            {
                string inputPath = video.FullName;
                string outputPath = @"D:\Droid2\rubicon.s01.e02.mp4";
                string arguments = string.Format("-i {0} -t 1 --angle 1 -c 1-28 -o {1} -f mp4 -X 848 -l 480 --crop 0:0:0:0 -e x264 -b 750 -a 1 -E faac -6 dpl2 -R 48 -B 160 -D 0.0 --subtitle 1 --subtitle-burn=1 -x level=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:no-fast-pskip=1:psy-rd=0,0:subq=6:8x8dct=0:trellis=0:weightb=0:mixed-refs=1 -v 1", inputPath, outputPath);
                
                Process proc = new Process();
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.CreateNoWindow = false;
                proc.StartInfo.FileName = "HandBrakeCLI.exe";
                proc.StartInfo.Arguments = arguments;
                proc.Start();

                string output = proc.StandardOutput.ReadToEnd();
                string error = proc.StandardError.ReadToEnd();
                proc.WaitForExit();               
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Occured: {0}, {1}", ex.Message, ex.StackTrace.ToString());                
            }

        }
    }
}
